package logic

import (
	"apple/models/mysql"
	"context"
	"fmt"
	"strings"

	"apple/applerpc/applerpc"
	"apple/applerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LeaveLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLeaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LeaveLogic {
	return &LeaveLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 留言
func (l *LeaveLogic) Leave(in *applerpc.LeaveRequest) (*applerpc.LeaveResponse, error) {
	// todo: add your logic here and delete this line
	var activity mysql.Activity
	mysql.DB.Debug().Where("id=?", in.Id).Find(&activity)
	if activity.ID == 0 {
		return nil, fmt.Errorf("账户不存在")
	}
	if strings.Contains(in.Message, "操") {
		return nil, fmt.Errorf("包含敏感词,不能发布")
	}
	activity.Message = in.Message

	mysql.DB.Debug().Updates(&activity)
	return &applerpc.LeaveResponse{
		Id: int64(activity.ID),
	}, nil
}
